'use strict';
/* 目前封装参数  event = {
	action  : add/get     //请求方式
	data: 数据体   //请求为add时需要添加的参数
	whereParam:  //筛选参数
	limitParam:  //限制参数
}
*/
exports.main = async (event, context) => {
	//event为客户端上传的参数
	if (event.collection) {
		const db = await uniCloud.database({
			event,
			context
		});
		const collection = db.collection(event.collection)
		if (event.action === "get") {
			if (event.whereParam) {
				if (event.limitParam) {
					if (event.fieldParam) {
						return collection.where(event.whereParam).limit(event.limitParam).field(event.fieldParam).get()
					} else {
						return collection.where(event.whereParam).limit(event.limitParam).get()
					}
				} else {
					if (event.fieldParam) {
						return collection.where(event.whereParam).field(event.fieldParam).get()
					} else {
						return collection.where(event.whereParam).get()
					}
				}
			} else {
				if (event.limitParam) {
					if (event.fieldParam) {
						return collection.limit(event.limitParam).field(event.fieldParam).get()
					} else {
						return collection.limit(event.limitParam).get()
					}
				} else {
					if (event.fieldParam) {
						return collection.field(event.fieldParam).get()
					} else {
						return collection.get()
					}
				}
			}
		} else if (event.action === "add") {
			if (event.whereParam) {
				if (event.limitParam) {
					return collection.where(event.whereParam).limit(event.limitParam).add(event.data)
				} else {
					return collection.where(event.whereParam).add(event.data)
				}
			} else {
				if (event.limitParam) {
					return collection.add(event.data)
				} else {
					return collection.limit(event.limitParam).add(event.data)
				}
			}
		} else if (event.action === "update") {
			if (event.whereParam) {
				if (event.limitParam) {
					return collection.where(event.whereParam).limit(event.limitParam).update(event.data)
				} else {
					return collection.where(event.whereParam).update(event.data)
				}
			} else {
				if (event.limitParam) {
					return collection.update(event.data)
				} else {
					return collection.limit(event.limitParam).update(event.data)
				}
			}
		}
	}
};